package cn.roadtrip.utilmodule.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * 取模辅助类
 */
public class ModHelper {
    public static final int modeSize = 8;

    /**
     * 字符串取模
     *
     * @param str
     * @return
     */
    public static Integer modString(String str) {
        int n = 0;
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        for (byte b : bytes) {
            n += b;
        }
        n = Math.abs(n);
        return n % modeSize;
    }


    public static Integer parseMonthIndexNum(Long datetime) {
        DateTime dateTime = DateUtil.parseDateTime(DateUtil.formatDateTime(new Date(datetime)));
        int year = dateTime.year();
        int month = dateTime.month() + 1;
        int n = year * 12 + month;
        return n;
    }

    /**
     * 解析每半月一次的索引序号
     *
     * @param datetime 日期时间
     * @return
     */
    public static Integer parseHalfMonthIndexNum(Long datetime) {
        DateTime dateTime = DateUtil.parseDateTime(DateUtil.formatDateTime(new Date(datetime)));
        int year = dateTime.year();
        int month = dateTime.month() + 1;
        int days = dateTime.dayOfMonth();
        System.out.println("year:" + year + "\tmonth:" + month + "\tdays:" + days);
        int n = year * 12 * 2 + month * 2 + (days < 16 ? 1 : 2);
        return n;
    }
}